layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;


	var table_filter = 'order_list',
		where_json = {
			_o: 'create_time',
			_b: 'desc'
		},
		form_filter = 'order_search_form';


	// 调用物流公司
	send_info(kuaidiList_url, {}, function(data) {
		var option_html = '<option value="">全部</option><option value="自配物流">自配物流</option>'
		data.data.forEach((e, i) => {
			option_html += `<option value="${e}">${e}</option>`
		})
		$('form[lay-filter="order_search_form"]').find('select[name="wuliu"]').html(option_html);
		form.render('select', 'order_search_form')
	})
	// 结束

	// 渲染表格
	layer.load();
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: order_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'create_time',
			_b: 'desc',
			need_supplier: 'yes',
			//exact:'yes'
		},
		defaultToolbar: ['filter'],
		totalRow: true,
		cols: [order_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 15,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#orderToolbar',
		initSort: {
			field: 'create_time',
			type: 'desc'
		},
		done: function() {
			layer.closeAll();

		}
	});

	// 交货时间
	var delivery_date = laydate.render({
		elem: 'input[name="order_form_delivery_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search('delivery_date', value);
		}
	});
	// 指派时间
	var push_supplier_time_start = laydate.render({
		elem: 'input[name="push_supplier_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				push_supplier_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('push_supplier_time_start', value);
		}
	});
	var push_supplier_time_end = laydate.render({
		elem: 'input[name="push_supplier_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				push_supplier_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('push_supplier_time_end', value);
		}
	});
	// （供）确认时间
	var supplier_confirm_time_start = laydate.render({
		elem: 'input[name="supplier_confirm_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				supplier_confirm_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('supplier_confirm_time_start', value);
		}
	});
	var supplier_confirm_time_end = laydate.render({
		elem: 'input[name="supplier_confirm_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				supplier_confirm_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('supplier_confirm_time_end', value);
		}
	});
	// 下单时间
	var create_time_start = laydate.render({
		elem: 'input[name="order_form_create_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="order_form_create_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('create_time_end', value);
		}
	});
	// 订单类型时间
	var order_type_time_start = laydate.render({
		elem: 'input[name="order_type_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				order_type_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('order_type_time_start', value);
		}
	});
	var order_type_time_end = laydate.render({
		elem: 'input[name="order_type_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				order_type_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('order_type_time_end', value);
		}
	});
	// 确认时间
	var confirm_time_start = laydate.render({
		elem: 'input[name="order_form_confirm_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				confirm_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('confirm_time_start', value);
		}
	});
	var confirm_time_end = laydate.render({
		elem: 'input[name="order_form_confirm_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				confirm_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('confirm_time_end', value);
		}
	});
	// 支付时间
	var pay_time_start = laydate.render({
		elem: 'input[name="order_form_pay_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('pay_time_start', value);
		}
	});
	var pay_time_end = laydate.render({
		elem: 'input[name="order_form_pay_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				pay_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('pay_time_end', value);
		}
	});
	// 发货时间
	var shopping_time_start = laydate.render({
		elem: 'input[name="order_form_shopping_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				shopping_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('shopping_time_start', value);
		}
	});
	var shopping_time_end = laydate.render({
		elem: 'input[name="order_form_shopping_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				shopping_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('shopping_time_end', value);
		}
	});
	// 收货时间
	var receipt_time_start = laydate.render({
		elem: 'input[name="order_form_receipt_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				receipt_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('receipt_time_start', value);
		}
	});
	var receipt_time_end = laydate.render({
		elem: 'input[name="order_form_receipt_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				receipt_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('receipt_time_end', value);
		}
	});
	// 供发货时间
	var gys_fh_time_start = laydate.render({
		elem: 'input[name="gys_fh_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_start', value);
		}
	});
	var gys_fh_time_end = laydate.render({
		elem: 'input[name="gys_fh_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_fh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_fh_time_end', value);
		}
	});
	// 军发货时间
	var jc_fh_time_start = laydate.render({
		elem: 'input[name="jc_fh_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				jc_fh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_fh_time_start', value);
		}
	});
	var jc_fh_time_end = laydate.render({
		elem: 'input[name="jc_fh_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				jc_fh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_fh_time_end', value);
		}
	});
	// 供收货时间
	var gys_sh_time_start = laydate.render({
		elem: 'input[name="gys_sh_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_start', value);
		}
	});
	var gys_sh_time_end = laydate.render({
		elem: 'input[name="gys_sh_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				gys_sh_time_end.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('gys_sh_time_end', value);
		}
	});

	// 用户发票时间
	var invoice_time_start = laydate.render({
		elem: 'input[name="invoice_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				invoice_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('invoice_time_start', value);
		}
	});
	var invoice_time_end = laydate.render({
		elem: 'input[name="invoice_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				invoice_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('invoice_time_end', value);
		}
	});
	// 上传采购发票时间
	var caigou_invoice_time_start = laydate.render({
		elem: 'input[name="caigou_invoice_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				caigou_invoice_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('caigou_invoice_time_start', value);
		}
	});
	var caigou_invoice_time_end = laydate.render({
		elem: 'input[name="caigou_invoice_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				caigou_invoice_time_end.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('caigou_invoice_time_end', value);
		}
	});
	// 供应商发票申请时间
	var apply_time_start = laydate.render({
		elem: 'input[name="apply_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_time_start', value);
		}
	});
	var apply_time_end = laydate.render({
		elem: 'input[name="apply_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_time_end', value);
		}
	});
	// 供应商发票审核时间
	var apply_shehe_time_start = laydate.render({
		elem: 'input[name="apply_shehe_time_start"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_shehe_time_end.config.min = {
					year: date.year,
					month: date.month - 1,

					date: date.date
				};
			}
			select_search('apply_shehe_time_start', value);
		}
	});
	var apply_shehe_time_end = laydate.render({
		elem: 'input[name="apply_shehe_time_end"]',
		max: 1,
		theme: '#393D49',
		type: 'datetime',
		done: function(value, date, endDate) {
			if (value) {
				apply_shehe_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('apply_shehe_time_end', value);
		}
	});
	// 军收货时间
	var jc_sh_time_start = laydate.render({
		elem: 'input[name="jc_sh_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_sh_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_sh_time_start', value);
		}
	});
	var jc_sh_time_end = laydate.render({
		elem: 'input[name="jc_sh_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				jc_sh_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('jc_sh_time_end', value);
		}
	});
	// 入库时间
	var ruku_time_start = laydate.render({
		elem: 'input[name="ruku_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ruku_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ruku_time_start', value);
		}
	});
	var ruku_time_end = laydate.render({
		elem: 'input[name="ruku_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ruku_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ruku_time_end', value);
		}
	});

	// 军付款时间
	var ghs_pay_time_start = laydate.render({
		elem: 'input[name="ghs_pay_time_start"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ghs_pay_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ghs_pay_time_start', value);
		}
	});
	var ghs_pay_time_end = laydate.render({
		elem: 'input[name="ghs_pay_time_end"]',
		max: 1,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				ghs_pay_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search('ghs_pay_time_end', value);
		}
	});

	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var order_id = data.order_id;
		var order_no = data.order_no;
		switch (e) {
			case 'viewMore':
				if (tr.last().find('i').hasClass('layui-icon-right')) {
					tr.last().find('i').removeClass('layui-icon-right').addClass('layui-icon-down');
					tr.after(
						'<tr data-index="-1"><td colspan="16" class="layui-bg-green" style="padding:1px 1px 1px 5px;"><table id="order_list_son' +
						order_id + '" lay-filter="order_list_son' + order_id +
						'"></table></td></tr>');
					table.render({
						elem: '#order_list_son' + order_id,
						id: 'order_list_son' + order_id,
						height: 'auto',
						width: 'auto',
						cols: [order_goods_cols],
						url: cart_detail_url,
						mothod: 'post',
						height: 'auto',
						where: {
							order_no: order_no
						}
					});
					table.on('edit(order_list_son' + order_id + ')', function(obj) {
						var field = obj.field,
							value = obj.value,
							cart_id = obj.data.cart_id,
							post_data = {
								field: field,
								value: value,
								cart_id: cart_id
							};
						send_info(save_cart_info_url, post_data, function(data) {
							layer.msg('保存成功', {
								icon: 1
							});
						});
					});
				} else {
					tr.last().find('i').removeClass('layui-icon-down').addClass('layui-icon-right');
					tr.next().remove();
				}
				break;
			case 'share_goods':
				layer.prompt({
					formType: 2,
					value: 'https://tailongji.dixintong.com' + share_goods_url + '?order_no=' +
						order_no,
					title: '分享商品地址',
					btn: ['复制'],
					area: ['500px', '40px'] //自定义文本域宽高
				}, function() {
					$('textarea.layui-layer-input').select();
					document.execCommand("Copy");
					layer.msg('复制成功')
					//layer.close(index);

				});
				break
			case 'share_contact':
				layer.prompt({
					formType: 2,
					value: 'https://tailongji.dixintong.com' + share_contact_url +
						'?order_no=' + order_no,
					title: '分享联系人信息',
					btn: ['复制'],
					area: ['500px', '40px'] //自定义文本域宽高
				}, function() {
					$('textarea.layui-layer-input').select();
					document.execCommand("Copy");
					layer.msg('复制成功')
					//layer.close(index);

				});
				break
			case 'option_log':
				layer.open({
					type: 2,
					anim: -1,
					title: '操作日志',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: true,
					area: '30%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: option_log_url + '?order_no=' + data.order_no
				})
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {

					send_info(order_delete_url, {
						order_id: order_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_order_list"]').length >
								0) {
								table.reload('recycle_order_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(order_one_url, {
					order_id: data.order_id
				}, function(data) {
					delete data.data.order_id;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	});

	$('#back_btn').on('click', function() {
		let post_json = {
			order_no: $('#back_order_no').val(),
			back_state: $('#back_state').val()
		};
		console.log(post_json);
		if (post_json.order_no == '') {
			layer.msg('输入订单号', {
				icon: 2
			});
			return false;
		}
		var index1 = layer.confirm('确定回滚订单:' + post_json.order_no + '吗?', {
			icon: 3,
			title: '提示',
			yes: function() {
				send_info(back_order_url, post_json, function(data) {

					layer.close(index1);
					layer.msg(data.data);
				})
			}
		})
	});

	// 合并采购发票
	var upload_mergeInvoice = upload.render({
		elem: '.mergeInvoice',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(mergeInvoice_uri + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.confirm('文件合并完成', {
						icon: 1,
						title: '温馨提示：',
						btn: ['下载合并文件']
					}, function(index) {
						location.href = data.count
					})
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 打包采购发票
	var upload_caigoufapiao = upload.render({
		elem: '.dabaocaigoufapiao',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(daobaocaigoufapiao_uri + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.confirm('文件打包完成', {
						icon: 1,
						title: '温馨提示：',
						btn: ['下载打包文件']
					}, function(index) {
						location.href = data.count
						console.log('下载')
					})
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导出采购合同
	var caigou = upload.render({
		elem: '.caigou',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(caigou_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.data == 'error') {
					layer.msg(data.count, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.confirm('文件打包完成', {
						icon: 1,
						title: '温馨提示：',
						btn: ['下载打包文件']
					}, function(index) {
						location.href = data.count
						console.log('下载')
					})
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导出采购合同1
	var caigou1 = upload.render({
		elem: '.caigou1',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(caigou1_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.data == 'error') {
					layer.msg(data.count, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.confirm('文件打包完成', {
						icon: 1,
						title: '温馨提示：',
						btn: ['下载打包文件']
					}, function(index) {
						location.href = data.count
						console.log('下载')
					})
				}

			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导入订单号导出采购合同
	var upload_downCght = upload.render({
		elem: '.downCght',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(downCght_url + '?file=' + res.data.save_path, {
				type: 'cght'
			}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.closeAll();
					layer.open({
						type: 2,
						title: '下载采购合同',
						content: data.data
					});
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导入订单号导出销售发票
	var upload_saleInvoice = upload.render({
		elem: '.saleInvoice',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(downCght_url + '?file=' + res.data.save_path, {
				type: 'xsfp'
			}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.closeAll();
					layer.open({
						type: 2,
						title: '下载销售发票',
						content: data.data
					});
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});

	// 导入订单号导出采购发票
	var upload_buyInvoice = upload.render({
		elem: '.buyInvoice',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(downCght_url + '?file=' + res.data.save_path, {
				type: 'cgfp'
			}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.closeAll();
					layer.open({
						type: 2,
						title: '下载采购发票',
						content: data.data
					});
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});


	// 导入第三方订单
	var upload_explodeThirdOrder = upload.render({
		elem: '.explodeThirdOrder',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			post_data = [];
			send_info(explodeThirdOrder_url + '?file=' + res.data.save_path, {}, function(data) {
				if (data.code == -1) {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				} else {
					layer.msg(data.msg, {
						icon: 1,
						anim: 6
					});
					table.reload(table_filter, where_json)
				}
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});


	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var add_url = $(this).attr('data-url');
		switch (obj.event) {
			case 'allFahuo':
				layer.confirm('确定要批量操作吗？', {
					yes: function() {
						layer.load(2);
						send_info(all_fahuo_url, where_json, function(data) {
							layer.closeAll();
							layer.alert(data.msg)
						})
					}
				})
				break;
			case 'allShouhuo':
				layer.confirm('确定要批量操作吗？', {
					yes: function() {
						layer.load(2);
						send_info(all_shouhuo_url, where_json, function(data) {
							layer.closeAll();
							layer.alert(data.msg)
						})
					}
				})
				break;
			case 'allTuipiao':
				layer.confirm('确定要批量操作吗？', {
					yes: function() {
						layer.load(2);
						send_info(all_tuipiao_url, where_json, function(data) {
							layer.closeAll();
							layer.alert(data.msg)
						})
					}
				})
				break;
			case 'backOrder':
				layer.open({
					type: 1,
					shade: false,
					shadeClose: true,
					area: ['40%', '500px'],
					title: '数据回滚', //不显示标题
					content: $('#back_data')
				});
				break;
			case 'exportSonghuodan':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的送货单', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				layer.confirm('生成PDF速度很慢，确定导出吗？', {
					yes: function() {
						order_no = new Array();
						for (var i in checkStatus.data) {
							order_no.push(checkStatus.data[i].order_id);
						}
						var order_ids = order_no.join(',')
						layer.closeAll();
						window.open(order_exportsonghuodan_url + '?order_ids=' + order_ids);
						return false;
						//layer.load(2);
						layer.closeAll();
						layer.open({
							type: 2,
							title: '正在导出PDF文件',
							content: order_exportsonghuodan_url + '?order_ids=' +
								order_ids,
							closeBtn: false,
							move: false,
							success: function() {
								layer.closeAll();
								console.log('执行');
							}
						});
					}
				});
				break;
			case 'exportAllOrder':
				layer.confirm('确定导出订单吗？', {
					yes: function() {
						layer.load(2);
						send_info(order_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'exportCwOrder':
				layer.confirm('确定导出本次查询的财务订单吗？', {
					yes: function() {
						layer.load(2);
						send_info(order_cw_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'exportAllOrderNew':
				layer.confirm('确定导出订单吗？', {
					yes: function() {
						layer.load(2);
						send_info(order_export_new_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'downFapiao':
				layer.confirm('确定导出本次查询的订单发票信息吗？', {
					yes: function() {
						layer.load(2);
						send_info(order_downfapiao_url, where_json, function(data) {
							layer.closeAll();
							if (data.code == 1) {
								layer.open({
									type: 2,
									title: '下载打包文件',
									content: data.data
								});
							} else {
								layer.msg(data.msg, {
									icon: 2,
									anim: 6
								});
							}

							/* layer.open({
								type: 2,
								title: '下载打包文件',
								content: data.data
							}); */
						})
					}
				});
				break;
			case 'exportAllGysInvoice':
				layer.confirm('确定导出采购发票吗？', {
					yes: function() {
						layer.load(2);
						send_info(invoice_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载采购发票excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'rukubiao':
				layer.confirm('确定导出昨日的入库表吗？', {
					yes: function() {
						layer.load(2);
						send_info(rukubiao_url, {}, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载入库表',
								content: data.data
							});
						})
					}
				});
				break;
			case 'exportAllSaleInvoice':
				layer.confirm('确定导出销售发票吗？', {
					yes: function() {
						layer.load(2);
						send_info(saleinvoice_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载销售发票excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'rukuexportOrder':
				layer.confirm('确定导出入库表吗？', {
					yes: function() {
						layer.load(2);
						send_info(rukuinvoice_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载入库excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'rukuWhereExportOrder':
				layer.confirm('确定导出入库表吗？', {
					yes: function() {
						layer.load(2);
						send_info(ruku_export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载入库excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'downFile':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('尚未选择', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				order_no = new Array();
				for (var i in checkStatus.data) {
					order_no.push(checkStatus.data[i].out_order_no);
				}
				layer.open({
					type: 2,
					content: zip_down_url + '?out_order_no=' + order_no
				});
				break;
			case 'exportOrder':
				var checkStatus = table.checkStatus(table_filter);
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要导出的订单', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				layer.confirm('确定要导出选中订单吗？', {
					yes: function() {
						order_no = new Array();
						for (var i in checkStatus.data) {
							order_no.push(checkStatus.data[i].order_no);
						}
						var export_json = {
							order_no: order_no,
							_o: 'order_id',
							_b: 'desc'
						};
						layer.load(2);
						send_info(order_export_url, export_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data,
								time: 500
							});
						})
					}
				});
				break;
			case 'addOrder':
				layer.open({
					type: 2,
					anim: -1,
					title: '新增竞价单',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: add_url
				})
				return false;
				break;
			case 'delOrder':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要删除的数据', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var order_id_arr = new Array();
				layer.confirm('确定删除选中数据吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						order_id_arr.push(obj.order_id);
					});
					send_info(order_delete_url, {
						order_id: order_id_arr
					}, function(data) {
						if (data.code == 0) {
							tr.map(function(obj, i) {
								$(obj.selector).remove();
							})
							layer.msg('删除成功', {
								icon: 1
							});
							// 重载回收站
							if ($('table[lay-filter="recycle_order_list"]').length >
								0) {
								table.reload('recycle_order_list');
							}
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					})
				})
				break;
			default:
				break;
		}
		return false;
	})

	// 点击查看详情
	table.on('row(' + table_filter + ')', function(obj) {
		if (obj.data.order_no == undefined) {
			return false;
		}
		var get_site_id = $('form[lay-filter="order_search_form"]').find('select[name="site_id"]')
			.val(),
			content_url = order_detail_url + '?order_no=' + obj.data.order_no;
		if (get_site_id != undefined && get_site_id != '') {
			content_url = content_url + '&site_id=' + get_site_id;
		}
		var node_obj = document.elementFromPoint(event.clientX, event.clientY);
		if ($(node_obj).hasClass('layui-icon-ok') == false) {
			layer.open({
				type: 2,
				anim: -1,
				title: '订单详情',
				closeBtn: true,
				offset: 'r',
				shade: 0.3,
				move: false,
				shadeClose: false,
				area: '95%',
				skin: "layui-anim layui-anim-rl layui-layer-adminRight",
				content: content_url
			})
		}
		return false;
	})




	// 排序
	table.on('sort(' + table_filter + ')', function(data) {
		var _o = data.field;
		var _b = data.type;
		table.reload(table_filter, {
			initSort: data,
			where: {
				_o: _o,
				_b: _b
			}
		})
	})
	form.render();
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		where_json[name] = value;
		/*
		layer.load();
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
		*/
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		switch (id) {
			case 'qingkuan_state':
				select_search('qingkuan_state', value);
				break;
			case 'zhuijia':
				select_search('zhuijia', value);
				break;
			case 'site_id':
				select_search('site_id', value);
				break;
			case 'order_type':
				select_search('order_type', value);
				break;
			case 'shouhou_type':
				select_search('shouhou_type', value);
				break;
			case 'jun_order_state':
				select_search('jun_order_state', value);
				break;
			case 'apply_state':
				select_search('apply_state', value);
				break;
			case 'returncode':
				select_search('returncode', value);
				break;
			case 'remark_type':
				select_search('remark_type', value);
				break;
			case 'member_remark_type':
				select_search('member_remark_type', value);
				break;
			case 'suppler_bz_type':
				select_search('suppler_bz_type', value);
				break;
			case 'wuliu':
				select_search('wuliu', value);
				break;
			case 'dingyue':
				select_search('dingyue', value);
				break;
				/* case 'exact':
					select_search('exact', value);
					break; */
			default:
				break;
		}
	});
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search(name, value);
	});
	$('#order_search_btn').on('click', function() {
		layer.load();
		//where_json.page = 1;
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		where_json[field] = value;
		/*
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
		*/
	};

	//同步发票
	var up_sny_invoice = upload.render({
		elem: '.up-sny-invoice',
		url: sny_invoice_uri,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			console.log(res);
			layer.closeAll('loading');
			layer.msg('同步成功', {
				icon: 1,
				anim: 6
			});
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	//生成合同
	var up_create_contract = upload.render({
		elem: '.up-create-contract',
		url: create_contract_url,
		accept: 'file',
		acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		exts: 'xls',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			console.log(res);
			layer.closeAll('loading');
			layer.msg(res.msg, {
				icon: res.code,
				anim: 6
			});
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});



	var edit_order = function(field, value, order_id) {
		var post = '{"order_id":"' + order_id + '","' + field + '":"' + value + '"}';
		var data = JSON.parse(post);
		send_info(order_save_url, data, function(data) {
			if (data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg('修改成功', {
					icon: 1
				});
			}
		})
	}
	//监听编辑
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			order_id = obj.data.order_id;
		order_adsense(field, value, order_id);
	});
	//导入入库
	var orderruku_id = upload.render({
		elem: '#orderruku',
		url: order_ruku_url,
		acceptMime: 'application/vnd.ms-excel',
		auto: true,
		accept: 'file',
		exts: 'xls',
		multiple: false,
		drag: false,
		choose: function(obj) {
			console.log('选择');
		},
		before: function(obj) {
			console.log('开始')
			layer.load();
		},
		done: function(res, index, upload) {
			if (res.code == 1) {
				layer.alert('数据同步成功', {
					icon: 1
				});
			} else {
				layer.alert('数据同步失败', {
					icon: 2
				})
			}
			layer.closeAll('loading');
		},
		error: function(index, upload) {
			console.log('错误');
			layer.msg('网络错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});


	//导入订单导出入库明细
	var exportRukuOfOrderXslid = upload.render({
		elem: '#exportRukuOfOrderXsl',
		url: exportRukuOfOrder_url,
		acceptMime: 'application/vnd.ms-excel',
		auto: true,
		accept: 'file',
		exts: 'xls',
		multiple: false,
		drag: false,
		choose: function(obj) {
			console.log('选择');
		},
		before: function(obj) {
			console.log('开始')
			layer.load();
		},
		done: function(res, index, upload) {
			console.log(res)
			if (res.code == 1) {
				layer.open({
					type: 2,
					title: '下载入库excel',
					content: res.data
				});
			} else {
				layer.alert('数据同步失败', {
					icon: 2
				})
			}
			layer.closeAll('loading');
		},
		error: function(index, upload) {
			console.log('错误');
			layer.msg('网络错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});



	// 初始化城市
	var initSelectEvent = function(obj) {
		var province = obj["province"],
			city = obj["city"],
			area = obj["district"];
		var province_ul = $('form[lay-filter="order_search_form"]').find('select[name="province"]');
		province_ul.html('<option value="">请选择省份</option>');
		for (var i in province) {
			var e = province[i];
			province_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
		}
		var p_name = '',
			c_name = '',
			//pid = data.value,
			city_data = [];
		// 选择城市
		form.render('select');
		form.on('select(province)', function(data) {
			province.forEach(function(e, i) {
				if (e.id == data.value) {
					p_name = e.name;
				}
			})
			select_search('pname', p_name);
			select_search('pname_id', data.value);
			city_data = city[data.value];
			var city_list = city[data.value];
			var city_ul = $('form[lay-filter="order_search_form"]').find('select[name="city"]');
			city_ul.html('<option value="">请选择</option>');
			for (var i in city_list) {
				var e = city_list[i];
				city_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
			}

			form.render('select');
		});
		form.on('select(city)', function(data) {
			city_data.forEach(function(e, i) {
				if (e.id == data.value) {
					c_name = e.name;
				}
			})
			select_search('cname', c_name);
			select_search('cname_id', data.value);
			var area_list = area[data.value];
			var area_ul = $('form[lay-filter="order_search_form"]').find('select[name="area"]');
			area_ul.html('<option value="">请选择</option>');
			for (var i in area_list) {
				var e = area_list[i];
				area_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
			}

			form.render('select');
		})
	}
	var ajaxConfig = {
		url: "/static/js/jqcity/allcity.js",
		dataType: "jsonp",
		jsonpCallback: "callback",
		success: initSelectEvent
	};
	$.ajax(ajaxConfig);
});